package com.xie.smfs.item;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.world.World;
import com.xie.smfs.common.events.PlayerEvents;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class BaseGhostEyeItem extends Item {
    private static final Logger LOGGER = LoggerFactory.getLogger(BaseGhostEyeItem.class);
    protected final int maxSpiritBonus;
    protected final int spiritResistanceBonus;
    protected final int spiritDamageBonus;

    public BaseGhostEyeItem(Settings settings, int maxSpiritBonus, int spiritResistanceBonus, int spiritDamageBonus) {
        super(settings);
        this.maxSpiritBonus = maxSpiritBonus;
        this.spiritResistanceBonus = spiritResistanceBonus;
        this.spiritDamageBonus = spiritDamageBonus;
    }

    @Override
    public TypedActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
        if (world.isClient) {
            // 添加客户端反馈音效和交互提示
            player.sendMessage(Text.literal("检测到右键交互，正在处理..."), false);
            return TypedActionResult.pass(player.getStackInHand(hand));
        }

        ItemStack stack = player.getStackInHand(hand);
        if (player instanceof ServerPlayerEntity serverPlayer) {
            LOGGER.info("玩家 {} 是服务器端玩家，开始处理绑定逻辑", serverPlayer.getGameProfile().getName());
            int emptySlot = PlayerEvents.getGhostSlot(serverPlayer);
            LOGGER.info("查找空槽位结果: {}", emptySlot == -1 ? "无可用槽位" : "找到空槽位: " + (emptySlot + 1));
            if (emptySlot != -1) {
                ItemStack ghostEyeCopy = stack.copy();
                ghostEyeCopy.setCount(1);

                try {
                    PlayerEvents.setGhostSlotData(serverPlayer, emptySlot, ghostEyeCopy);
                    applySpiritAttributes(serverPlayer);
                    stack.decrement(1);
                    PlayerEvents.validateGhostSlots(serverPlayer);

                    player.sendMessage(Text.literal("成功将灵异装备绑定到槽位 " + (emptySlot + 1)), false);
                    LOGGER.info("成功将灵异装备绑定到玩家 {} 的槽位 {} 中", serverPlayer.getGameProfile().getName(), emptySlot + 1);
                    return TypedActionResult.success(stack);
                } catch (Exception e) {
                    LOGGER.error("绑定鬼眼到槽位 {} 失败: {}", emptySlot, e.getMessage(), e);
                    player.sendMessage(Text.literal("绑定灵异装备失败，请重试"), false);
                    return TypedActionResult.fail(stack);
                }
            } else {
                sendSlotsFullMessage(player);
                return TypedActionResult.fail(stack);
            }
        }
        return TypedActionResult.pass(stack);
    }

    protected void applySpiritAttributes(ServerPlayerEntity player) {
        NbtCompound spiritData = PlayerEvents.getSpiritAttributes(player);
        spiritData.putDouble("maxSpirit", spiritData.getDouble("maxSpirit") + maxSpiritBonus);
        spiritData.putDouble("spiritResistance", spiritData.getDouble("spiritResistance") + spiritResistanceBonus);
        spiritData.putDouble("spiritDamage", spiritData.getDouble("spiritDamage") + spiritDamageBonus);
        PlayerEvents.setSpiritAttributes(player, spiritData);
    }

    protected abstract Text getBindSuccessMessage(int slot);
    protected abstract void sendSlotsFullMessage(PlayerEntity player);
}